import type { Request } from 'express'
import type { TodoQuery } from '@data/todo'
import type { ApiResponse } from '@data/api'
import { todoService } from '@server/services/todo-service'

export default async function getTodoList(request: Request) {
  const query: TodoQuery = {
    groupId: request.query.groupId as string,
    completed: request.query.completed ? request.query.completed === 'true' : undefined,
    deleted: request.query.deleted ? request.query.deleted === 'true' : undefined,
    priority: request.query.priority as 'high' | 'medium' | 'low' | 'none' | undefined,
    search: request.query.search as string,
    startDate: request.query.startDate as string,
    endDate: request.query.endDate as string,
    tags: request.query.tags ? (request.query.tags as string).split(',') : undefined,
  }

  const todos = await todoService.getTodos(query)

  const response: ApiResponse = {
    success: true,
    data: todos,
  }

  return response
}
